Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Thread BR extended address in WS API calls #21172

Merged
merged 7 commits into from
Jul 17, 2024

Conversation

agners
Copy link
Member

@agners agners commented Jun 26, 2024

Breaking change

Proposed change

This follows the HA Core change and passes the extended address to the OTBR WS API calls where necessary. It also follows the new OTBR info format which potentially includes multiple OTBRs.

This allows to support multiple OTBR managed by a single system.

Note: There is one corner case when none of the OTBR is found via discovery. In this case we offer to reset the OTBR. Currently we simply offer this for the primary or first one found.

Related Core PR: home-assistant/core#108282

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Additional information

  • This PR fixes or closes issue: fixes #
  • This PR is related to issue or discussion:
  • Link to documentation pull request:

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

Summary by CodeRabbit

  • New Features

    • Enhanced Thread network configuration with added functionality for handling OTBR information more comprehensively.
  • Bug Fixes

    • Improved handling of extended addresses in network creation, setting, and channel configuration.
  • Documentation

    • Updated translation strings for clearer descriptions of Thread network actions.

This follows the HA Core change and passes the extended address to the
OTBR WS API calls where necessary. It also follows the new OTBR info
format which potentially includes multiple OTBRs.

This allows to support multiple OTBR managed by a single system.

Note: There is one corner case when none of the OTBR is found via
discovery. In this case we offer to reset the OTBR. Currently we simply
offer this for the primary or first one found.
@agners agners added the Feature PRs that are adding a new feature label Jun 26, 2024
@agners agners requested a review from bramkragten June 26, 2024 13:01
Copy link
Contributor

coderabbitai bot commented Jun 26, 2024

Walkthrough

Walkthrough

The updates involve enhancements to the OTBRInfo functionality, including introducing the extended_pan_id property, transitioning to OTBRInfoDict type, and modifying several methods to accept extended_address parameter. These changes impact src/data/otbr.ts for data handling and thread-config-panel.ts for configuration panel logic. Additionally, new translation keys were added to en.json.

Changes

File Path Change Summary
src/data/otbr.ts Added extended_pan_id to OTBRInfo, changed return type of getOTBRInfo to OTBRInfoDict, and modified methods to accept extended_address.
src/panels/config/integrations/.../thread/thread-config-panel.ts Updated to use OTBRInfoDict, introduced otbrForNetwork, and refactored methods to handle otbr information.
src/translations/en.json Added translation keys for "thread_network_info" and "thread_network_delete_credentials".

Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between db091aa and 881e762.

Files selected for processing (1)
  • src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts (15 hunks)
Additional context used
Biome
src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts

[error] 212-212: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

Additional comments not posted (1)
src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts (1)

79-79: Change in State Property Type

The _otbrInfo state property type has been changed from OTBRInfo to OTBRInfoDict. This aligns with the PR's objective to handle multiple OTBRs. Ensure that all references to _otbrInfo throughout the codebase are updated to reflect this change.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

Outside diff range comments (1)
src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts (1)

Line range hint 463-481: Specify a more accurate type than any for otbr.

Using any reduces type safety. Consider specifying a more accurate type for better type checking and maintainability.

- const otbr = (ev.currentTarget as any).otbr as OTBRInfo;
+ const otbr = (ev.currentTarget as HTMLElement).otbr as OTBRInfo;
Tools
Biome

[error] 472-472: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 487-487: Unexpected any. Specify a different type. (lint/suspicious/noExplicitAny)

any disables many type checking rules. Its use should be avoided.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

Outside diff range comments (4)
src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts (4)

Line range hint 491-504: Improve error handling in _resetBorderRouter.

The method uses any for error handling, which reduces type safety. Consider specifying a more accurate type for the error and enhance error handling to manage specific error cases.

try {
  await OTBRCreateNetwork(this.hass, otbr.extended_address);
} catch (err: unknown) {
  showAlertDialog(this, {
    title: this.hass.localize("ui.panel.config.thread.otbr_config_failed"),
    text: (err as Error).message,
  });
  // Log error or handle specific error cases here
}
Tools
Biome

[error] 472-472: Forbidden non-null assertion. (lint/style/noNonNullAssertion)


[error] 487-487: Unexpected any. Specify a different type. (lint/suspicious/noExplicitAny)

any disables many type checking rules. Its use should be avoided.


Line range hint 612-644: Improve error handling in _changeChannel.

The method uses any for error handling, which reduces type safety. Consider specifying a more accurate type for the error and enhance error handling to manage specific error cases.

try {
  const result = await OTBRSetChannel(this.hass, otbr.extended_address, channel);
  showAlertDialog(this, {
    title: this.hass.localize("ui.panel.config.thread.change_channel_initiated_title"),
    text: this.hass.localize("ui.panel.config.thread.change_channel_initiated_text", { delay: Math.floor(result.delay / 60) }),
  });
} catch (err: unknown) {
  if (err instanceof CustomError) { // Assume CustomError is a more specific error type you might expect
    showAlertDialog(this, {
      title: "Specific Error",
      text: err.message,
    });
  } else {
    showAlertDialog(this, {
      title: "Error",
      text: (err as Error).message,
    });
  }
}

Line range hint 514-533: Improve error handling in _setDataset.

The method uses any for error handling, which reduces type safety. Consider specifying a more accurate type for the error and enhance error handling to manage specific error cases.

try {
  await OTBRSetNetwork(this.hass, otbr.extended_address, preferedDatasetId);
} catch (err: unknown) {
  showAlertDialog(this, {
    title: this.hass.localize("ui.panel.config.thread.otbr_config_failed"),
    text: (err as Error).message,
  });
  // Log error or handle specific error cases here
}
Tools
Biome

[error] 533-533: Unexpected any. Specify a different type. (lint/suspicious/noExplicitAny)

any disables many type checking rules. Its use should be avoided.


Line range hint 654-654: Specify a more accurate type than any for ev.

Using any reduces type safety. Consider specifying a more accurate type for better type checking and maintainability.

- const dataset = (ev.currentTarget as any).networkDataset as ThreadDataSet;
+ const dataset = (ev.currentTarget as HTMLElement).networkDataset as ThreadDataSet;

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

Outside diff range comments (2)
src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts (2)

Line range hint 211-254: Complex logic in _renderNetwork needs refactoring.

The function _renderNetwork contains complex logic to determine the OTBR for a network. Consider refactoring to reduce complexity and improve maintainability.

- const otbr = this._otbrInfo && this._otbrInfo[router.extended_address];
- const showOverflow = ("dataset" in network && router.border_agent_id) || otbr;
+ const otbr = this._otbrInfo?.[router.extended_address];
+ const showOverflow = network.dataset && router.border_agent_id || otbr;
Tools
Biome

[error] 212-212: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


[error] 210-210: Excessive complexity detected.

Please refactor this function to reduce its complexity score from 31 to the max allowed complexity 15.

(lint/complexity/noExcessiveCognitiveComplexity)


Line range hint 471-497: Specify a more accurate type than any for event handling and improve error handling in border router methods.

Using any reduces type safety. Consider specifying a more accurate type for better type checking and maintainability. Also, add specific error handling for the API call in _resetBorderRouter.

- const otbr = (ev.currentTarget as any).otbr as OTBRInfo;
+ const otbr = (ev.currentTarget as HTMLElement).otbr as OTBRInfo || null;
if (!otbr) {
  return;
}
try {
  await OTBRCreateNetwork(this.hass, otbr.extended_address);
} catch (err: any) {
  showAlertDialog(this, {
    title: this.hass.localize("ui.panel.config.thread.otbr_config_failed"),
    text: err.message,
  });
  // Log error or handle specific error cases here
}
Tools
Biome

[error] 478-478: Forbidden non-null assertion.

(lint/style/noNonNullAssertion)


[error] 493-493: Unexpected any. Specify a different type.

any disables many type checking rules. Its use should be avoided.

(lint/suspicious/noExplicitAny)

@frenck frenck added the smash Indicator this PR is close to finish for merging or closing label Jul 4, 2024
@agners agners requested a review from bramkragten July 16, 2024 07:06
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

Outside diff range, codebase verification and nitpick comments (4)
src/panels/config/integrations/integration-panels/thread/thread-config-panel.ts (4)

Line range hint 211-254: Refactor: Improve OTBR Handling in Router Display

When displaying routers, the code retrieves OTBR information based on the router's extended address. This could be simplified and made safer with optional chaining.

- this._otbrInfo && this._otbrInfo[router.extended_address];
+ this._otbrInfo?.[router.extended_address];

This change ensures that the code does not break if _otbrInfo is undefined.

Tools
Biome

[error] 212-212: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


Line range hint 471-487: Type Safety and Error Handling in Router Action Handling

The _handleRouterAction method uses type assertions that can be improved for better type safety. Additionally, error handling could be enhanced.

- const otbr = (ev.currentTarget as any).otbr as OTBRInfo;
+ const otbr = (ev.currentTarget as HTMLElement).otbr as OTBRInfo;
if (!otbr) {
  return;
}

This change ensures that the method does not proceed if otbr is undefined, preventing potential runtime errors.


Line range hint 492-510: Error Handling in Border Router Reset

The _resetBorderRouter method lacks specific error handling for the API call. Adding detailed error handling can improve user experience and debuggability.

try {
  await OTBRCreateNetwork(this.hass, otbr.extended_address);
} catch (err: any) {
  showAlertDialog(this, {
    title: this.hass.localize("ui.panel.config.thread.otbr_config_failed"),
    text: err.message,
  });
+ // Log error or handle specific error cases here
}

Adding comments or specific error handling strategies can help maintain and debug this method more effectively.


Line range hint 618-650: Channel Change Validation and Error Handling

The _changeChannel method allows for changing the communication channel of an OTBR. This method could benefit from additional validations and error handling to ensure the channel number is within the valid range before making the API call.

const channel = parseInt(channelStr);
+ if (channel < 11 || channel > 26) {
+   showAlertDialog(this, {
+     title: this.hass.localize("ui.panel.config.thread.change_channel_invalid"),
+     text: this.hass.localize("ui.panel.config.thread.change_channel_range"),
+   });
+   return;
+ }
try {
  const result = await OTBRSetChannel(this.hass, otbr.extended_address, channel);
  showAlertDialog(this, {
    title: this.hass.localize("ui.panel.config.thread.change_channel_initiated_title"),
    text: this.hass.localize("ui.panel.config.thread.change_channel_initiated_text", { delay: Math.floor(result.delay / 60) }),
  });
} catch (err: any) {
  showAlertDialog(this, {
    title: "Error",
    text: err.message || err,
  });
}

This change ensures that invalid channel numbers are caught early, preventing unnecessary API calls and potential errors.

@agners agners changed the title Use extended address in WS API calls Use Thread BR extended address in WS API calls Jul 16, 2024
@agners agners merged commit 7468ab9 into home-assistant:dev Jul 17, 2024
13 checks passed
@agners agners deleted the adjust-for-new-otbr-ws-api branch July 17, 2024 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla-signed Feature PRs that are adding a new feature smash Indicator this PR is close to finish for merging or closing wait for backend
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants